package test.serverpush;

import java.io.DataInputStream;
import java.io.DataOutputStream;
import java.io.IOException;
import java.net.InetSocketAddress;
import java.net.ServerSocket;
import java.net.Socket;

public class SimpleServer {
    public static void main(String[] args) throws IOException {
    	 ServerSocket serverSocket = new ServerSocket();
    	   serverSocket.bind(new InetSocketAddress("localhost", 20340));
    	   Socket socket = serverSocket.accept();
    	   String POLICY_REQUEST = "<policy-file-request/>\u0000";
    	   String POLICY_FILE = "<?xml version=\"1.0\"?>\n" +
    	      "<!DOCTYPE cross-domain-policy SYSTEM \n" +
    	      "http://www.adobe.com/xml/dtds/cross-domain-policy.dtd\">\n" +
    	      "<cross-domain-policy> \n" +
    	      " <allow-access-from domain=\"*\" to-ports=\"20340\"/> \n" +
    	      "</cross-domain-policy>";
    	   byte[] b = new byte[POLICY_REQUEST.length()];
    	   DataInputStream dataInputStream = new DataInputStream(socket.getInputStream());
    	   dataInputStream.readFully(b);
    	   String request = new String(b);
    	   if (POLICY_REQUEST.equals(request)) {
    	       DataOutputStream dataOutputStream = new DataOutputStream(socket.getOutputStream());
    	       dataOutputStream.write(POLICY_FILE.getBytes());
    	       dataOutputStream.flush();
    	       dataOutputStream.close();
    	   } else throw new IllegalArgumentException("unknown request format " + request);
    }
}